﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI.Methods
{
   public class listSubscribe : Api_BaseExecute
   {
      private new listSubscribeInput Input { get; set; }
      private listSubscribeOutput Output { get; set; }
      // =============================================== constructors
      public listSubscribe() { }
      // ---------
      public listSubscribe( listSubscribeInput input ) { Input = input; }
      // =============================================== Execute
      public listSubscribeOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public listSubscribeOutput Execute( listSubscribeInput input )
      {
         Input = input;
         Output = new listSubscribeOutput( Input );
         ValidationIsOK = true;
         //------------------------- validate it
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls
      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            string email_type = Input.parms.email_type.ToString();
            XmlRpcStruct merge_vars = apiXmlRpc.FormatMemberMergeVars( Input.parms.merge_vars );
            // ---------------------------------- rpc call -------------------------------------------
            bool rpcResult = apiXmlRpc.api.listSubscribe( Input.parms.apikey, Input.parms.id, Input.parms.email_address, merge_vars,
               email_type, Input.parms.double_optin, Input.parms.update_existing, Input.parms.replace_interests, Input.parms.send_welcome );
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "id", Input.parms.id );
            dict.Add( "email_address", Input.parms.email_address );
            dict.Add( "email_type", Input.parms.email_type.ToString() );
            dict.Add( "double_optin", Input.parms.double_optin.ToString() );
            dict.Add( "update_existing", Input.parms.update_existing.ToString() );
            dict.Add( "replace_interests", Input.parms.replace_interests.ToString() );
            dict.Add( "send_welcome", Input.parms.send_welcome.ToString() );
            // --------------------------------------
            if ( Input.parms.merge_vars.Count > 0 )
            {
               Dictionary<string, string> merge_vars =
                  apiSerial.FormatMemberMergeVars( Input.parms.merge_vars );
               foreach ( KeyValuePair<string, string> mv in merge_vars )
               {
                  dict.Add( string.Format( "merge_vars{0}", mv.Key ), mv.Value ?? "" );
               }
            }
            else
               dict.Add( "merge_vars[]", "" );     // hack to get around error if no merge_vars
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion

   }
}
